package com.eqdd.yiqidian.ui.mine.setting;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.eqdd.yiqidian.FeedbackActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.ThirdBean;
import com.eqdd.yiqidian.bean.rebuild.FeedBackListResultBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

public class FeedbackActivity extends BaseActivity {
    private FeedbackActivityCustom dataBinding;
    private ArrayList<Object> datas;
    private SlimAdapterEx<FeedBackListResultBean> slimAdapterEx;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_feedback);
        initTopTitleBar(View.VISIBLE, "帮助与反馈");
        initTopRightText("添加反馈", v -> {

            Bundle bundle = new Bundle();
            bundle.putString(Config.TITLE, "添加反馈");
            startActivityForResult(AddFeedbackActivity.class, bundle, Config.ADD_FEEDBACK);
        });
    }

    @Override
    public void initData() {
        datas = new ArrayList<>();
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_62, new SlimInjector<FeedBackListResultBean>() {
            @Override
            public void onInject(FeedBackListResultBean data, IViewInjector injector) {
                injector.text(R.id.tv_title, data.getFbackTitle())
                        .text(R.id.tv_time, data.getStatus() == 0 ? "未处理" : "已处理")
                        .text(R.id.tv_content, data.getFbackType() + " " + data.getCreateTime())
                        .with(R.id.tv_content, v -> {
                            TextView textView = (TextView) v;
                            textView.setMaxLines(1);
                            textView.setEllipsize(TextUtils.TruncateAt.END);
                        }).with(R.id.tv_time, view -> view.setRotation(-30));
            }
        }).attachTo(dataBinding.recyclerView).updateData(new ArrayList());
        ItemClickSupport.addTo(dataBinding.recyclerView)
                .setOnItemClickListener((recyclerView, position, v) -> {
                    Bundle bundle = new Bundle();
                    bundle.putSerializable(Config.FEEDBACK_INFO, slimAdapterEx.getDataItem(position));
                    startActivity(FeedBackInfoActivity.class, bundle);
                });
    }

    @Override
    public void setView() {
        OkGo.<HttpResult<List<FeedBackListResultBean>>>post(HttpConfig.BASE_URL + HttpConfig.FEEDBACK_LIST)
                .params("userGuid", user.getGuid())
                .execute(new DialogCallBack<HttpResult<List<FeedBackListResultBean>>>(FeedbackActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult<List<FeedBackListResultBean>>> response) {
                        HttpResult<List<FeedBackListResultBean>> httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {

                            slimAdapterEx.updateData(httpResult.getItems());
                        }
                    }
                });

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Config.ADD_FEEDBACK && resultCode == Config.SUCCESS) {
            setView();
        }
    }
}
